package com.tg.service.eventbus;

import com.google.common.eventbus.EventBus;
import com.google.common.eventbus.Subscribe;
import com.tg.service.suzakuEventBus.handler.MqEventHandler;
import com.tg.service.suzakuEventBus.handler.TaskEventHandler;
import com.tg.service.utils.ContextUtil;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by linzc on 2018/9/20.
 */
//@DependsOn({"MqEventHandler"})
//@Component
public class EventSubscribeBeanPostProcessor implements BeanPostProcessor {
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName)
            throws BeansException {

        return bean;
    }

    @Autowired
    private MqEventHandler mqEventHandler;

    //对于每个容器执行了初始化的 bean，如果这个 bean 的某个方法注解了@Subscribe,则将该 bean 注册到事件总线
    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        System.out.println(beanName);

        if (beanName.toLowerCase().contains("MqEventHandler2".toLowerCase())) {
            String eventName = "Login";

            TaskEventHandler handler1 = ContextUtil.getBean(eventName + TaskEventHandler.class.getSimpleName());
//            handler1.setBeanName(eventName);

//            String eventName2 = "Login";
//            TaskEventHandler handler2 = ContextUtil.getBean(eventName2 + TaskEventHandler.class.getSimpleName());
//            handler2.setBeanName(eventName2);

//            MqEventHandler2 mqEventHandler = (MqEventHandler2) bean;
//            mqEventHandler.setEventBusListener(Lists.newArrayList(handler1));
//            mqEventHandler.goon();
        }

//        if (beanName.toLowerCase().endsWith("taskeventhandler")) {
//            mqEventHandler.add((TaskEventHandler) bean);
//
//        }


        // for each method in the bean
        Method[] methods = bean.getClass().getMethods();
        for (Method method : methods) {
            // check the annotations on that method
            Annotation[] annotations = method.getAnnotations();
            for (Annotation annotation : annotations) {
                // if it contains the Subscribe annotation
                if (annotation.annotationType().equals(Subscribe.class)) {
                    // 如果这是一个Guava @Subscribe注解的事件监听器方法，说明所在bean实例
                    // 对应一个Guava事件监听器类，将该bean实例注册到Guava事件总线
                    List<TaskEventHandler> eventHandlerList = mqEventHandler.getHandlers();

                    eventHandlerList.forEach(eventHandler -> {
                        EventBus eventBus = (EventBus) eventHandler;
                        eventBus.register(bean);
                    });

                    return bean;
                }
            }
        }

        return bean;
    }
}
